package com.verisette.ncos.service.card.bizunit;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;
import com.verisette.ncos.web.utilities.SendNotifyMailThread;

public class NotifyPrintByEmailBU extends CommonBusinessUnit {
	private Logger logger = Logger.getLogger(NotifyPrintByEmailBU.class);
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		
		MasterUserProfile sender = (MasterUserProfile)processContext.getUserBean();
		
		TransCustomerOrder order = (TransCustomerOrder)processContext.getValueObject();
		SendNotifyMailThread sendMailThread = new SendNotifyMailThread();
		sendMailThread.setMailTemplate("mail-print-notify");
		sendMailThread.setSubjectContent("ดำเนินการพิมพ์แล้ว");
		sendMailThread.setOrder(order);
		sendMailThread.setSender(sender);
		
		if(logger.isDebugEnabled()){
			StringBuilder sdebug = new StringBuilder("orderId:");
			sdebug.append(order.getOrderId());
			sdebug.append("approved02:").append(order.getApproved02UspId());
			logger.debug(sdebug.toString());
		}
		
//		MasterUserProfile receiver = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getApproved02UspId());
//		sendMailThread.setReceiver(receiver);
//		sendMailThread.addEmailToReceivers(receiver.getUspEmail());
//		
//		MasterUserProfile approver01 = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getApprovedUspId());
//		if(approver01!=null) {
//			sendMailThread.addEmailToReceivers(approver01.getUspEmail());
//		}
		
		MasterUserProfile creator = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getCreatedUspId());
		if(creator!=null){
			sendMailThread.addEmailToReceivers(creator.getUspEmail());		
		}
		
		MasterMember member = order.getMasterMember();
		String requesterName = member.getMmbFnameTh(), requesterLname = member.getMmbLnameTh();
		if( StringUtils.isBlank(requesterName)){
			requesterName = member.getMmbFnameEn();
			requesterLname = member.getMmbLnameEn();
		}
		
		String name = "ไม่ทราบชื่อ";
		if(StringUtils.isNotBlank(creator.getFirstName())){
			name = creator.getFirstName();
			if(StringUtils.isNotBlank(creator.getLastName())){
				name = name + " " + creator.getLastName();
			}
		}
		sendMailThread.addContentParam(name);
		
		StringBuilder sbf = new StringBuilder(creator.getFirstName());
		sbf.append(" ").append(creator.getLastName());
		sbf.append(" (").append(requesterName);
		sbf.append(" ").append(requesterLname).append(") ");
		sendMailThread.addContentParam(sbf.toString());
		
		sendMailThread.start();
	}
}